import 'package:ai/AppRouter.dart';
import 'package:ai/AppRouter.gr.dart';
import 'package:ai/util/CommonUtils.dart';
import 'package:ai/util/SharedPreferencesUtil.dart';
import 'package:ai/util/ToastUtils.dart';
import 'package:ai/util/event_stream_util.dart';
import 'package:auto_route/annotations.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../../config/Config.dart';
import '../../entity/TextMessage.dart';

@RoutePage()
class TextWritePage extends StatefulWidget {
  final String taskId;

  final int textAppId;

  const TextWritePage({super.key, required this.taskId,required this.textAppId});

  @override
  State<TextWritePage> createState() => _TextWritePageState();
}

class _TextWritePageState extends State<TextWritePage> {
  final _controller = ScrollController();

  late String content = "";

  late bool isEnd = true;

  @override
  void initState() {
    super.initState();
    send();
  }

  void send() {
    setState(() {
      content = "";
    });

    EventStreamManager.send("${Config.baseUrl}/member/write/load", {
      'taskId': widget.taskId,
    }, (TextMessage message) {
      setState(() {
        isEnd = false;
        content = content + message.content;
      });
      _controller.jumpTo(_controller.position.maxScrollExtent);
    }, () {
      setState(() {
        isEnd = true;
      });
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        centerTitle: true,
        title: Text(
          "创作结果",
          style: TextStyle(
            color: Config.color39,
          ),
        ),
        leading: BackButton(
          color: Config.color39,
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
        actions: [
          TextButton(
            style: ButtonStyle(
              textStyle: WidgetStateProperty.all(
                const TextStyle(
                  color: Colors.red,
                ),
              ),
            ),
            onPressed: () {
              print("abc");
              CommonUtils.go(context, TextWriteRecord(textAppId: widget.textAppId));
            },
            child: Text(
              "历史记录",
              style: TextStyle(
                color: Config.color43,
              ),
            ),
          ),
          const SizedBox(
            width: 16,
          )
        ],
      ),
      body: Padding(
        padding: const EdgeInsets.all(8),
        child: Container(
          padding: const EdgeInsets.all(8),
          child: ListView(
            controller: _controller,
            children: [
              Container(
                decoration: BoxDecoration(
                  borderRadius: const BorderRadius.all(Radius.circular(8)),
                  color: Config.colorF8,
                ),
                child: Column(
                  children: [
                    content.isNotEmpty
                        ? Container(
                            padding: const EdgeInsets.only(
                                left: 8, right: 8, top: 8),
                            child: RichText(
                              text: TextSpan(
                                  text: content,
                                  style: TextStyle(
                                    color: Config.color34,
                                  ),
                                  children: <InlineSpan>[
                                    const WidgetSpan(
                                      child: SizedBox(
                                        width: 4,
                                      ),
                                    ),
                                    WidgetSpan(
                                      child: SizedBox(
                                        width: 16,
                                        height: 16,
                                        child: isEnd
                                            ? null
                                            : CircularProgressIndicator(
                                                strokeWidth: 2,
                                                color: Config.primaryColor,
                                              ),
                                      ),
                                    ),
                                  ]),
                            ),
                          )
                        : Container(),
                    Container(
                      width: double.infinity,
                      padding: const EdgeInsets.all(16),
                      child: Text(
                        "当前结果为AI合成。仅供参考",
                        style: TextStyle(
                          fontSize: 10,
                          color: Config.colorA8,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
      bottomNavigationBar: BottomAppBar(
        color: Colors.white,
        child: Row(
          children: [
            FilledButton(
              style: ButtonStyle(
                backgroundColor: WidgetStateProperty.all(Config.primaryColor),
              ),
              onPressed: () {
                SharedPreferencesUtil().getString("token");
              },
              child: const Text("保存"),
            ),
            const SizedBox(
              width: 8,
            ),
            Expanded(
              child: FilledButton(
                style: ButtonStyle(
                  backgroundColor: WidgetStateProperty.all(Config.primaryColor),
                ),
                onPressed: () {
                  Clipboard.setData(
                    ClipboardData(text: content),
                  );
                  ToastUtils.success(context, "复制成功");
                },
                child: const Text("复制全文"),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
